package com.longxi.tianjintianbao.ui.adapter.multitype.work;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.work.News;
import com.longxi.tianjintianbao.model.work.NewsList;

import java.util.List;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class NewViewBinder extends ItemViewBinder<NewsList, NewViewBinder.ViewHolder> {

    AdapterListener listener;
    private static NewViewBinder newViewBinder;
    private static ViewFlipper mFilpper;

    public static synchronized NewViewBinder instance() {
        if (newViewBinder == null) {
            newViewBinder = new NewViewBinder();
        }
        return newViewBinder;
    }

    public NewViewBinder() {
    }

    public NewViewBinder(AdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_viewflipper, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull NewsList news) {
        for (int i = 0; i < news.getNews().size(); i++) {
            View view = LayoutInflater.from(holder.mFilpper.getContext()).inflate(R.layout.item_new, null);
            TextView textView = view.findViewById(R.id.tvNewTitle);
            textView.setText(news.getNews().get(i).getTitle());
            holder.mFilpper.addView(view);
        }
        if(mFilpper != null && !mFilpper.isFlipping()){
            mFilpper.startFlipping();
        }else{
            holder.mFilpper.startFlipping();
            this.mFilpper = holder.mFilpper;
        }
        holder.mFilpper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(holder.getAdapterPosition());
            }
        });
    }

    /**
     * 轮播开始
     */
    public static void onResume() {
        if(mFilpper != null && !mFilpper.isFlipping()){
            mFilpper.startFlipping();
        }
    }

    /**
     * 轮播停止
     */
    public static void onPause() {
        if(mFilpper != null && mFilpper.isFlipping()){
            mFilpper.stopFlipping();
        }
    }

    public static void onDestroy(){
        mFilpper = null;
        newViewBinder = null;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ViewFlipper mFilpper;
        ViewHolder(View itemView) {
            super(itemView);
            mFilpper = itemView.findViewById(R.id.filpper);
        }
    }

}
